<?php 
$db=new SQLiteDatabase("db/mandi");
$response = false;
$load=$db->query("select sum(s.weight) from sale_table as s");
if($load){
	$load=$load->fetch(SQLITE_ASSOC);
	$load = $load['sum(s.weight)'];
	$percent_load = ($load / 80000) * 100.0;
	$remaining = 80000.0 - $load;
	if($remaining > 5){
		$response = sprintf(round($percent_load))."% mandi capacity remaining with space for ";
		$response .= sprintf(round($remaining))." Quintals as of ";
		$response .= date("G:i j F Y");
		}
	else{
		$response = "Sorry, mandi is full at ".sprintf(round($percent_load))."% capacity as of ";
		$response .= date("G:i j F Y")." Please try again later.";
		}
	}
	$response = utf8_encode($response);

if(!$response){
	$response = "To get price, SMS \"price <produce type> <recent, avg, high,low or total (Optional).";
	$response .= " Example: \"price peas recent\". For mandi capacity, SMS \"capacity\" ";
	$response = utf8_encode($response);
	//$response = utf8_encode($command);
	}
	
$http_date = gmdate("D, d M Y H:i:s T"); 
$n = strlen($response); 
header("Date: " . $http_date); 
header("Content-Type: text/plain"); 
header("Content-Length: " . $n); 
print $response; 
?>